<template>
  <div style="--van-grid-item-content-padding: 0px">
    <van-cell-group inset class="" title="基本信息">
      <van-cell title="订单号" :value="demandInfo.ryD_DemandId" />
      <van-cell title="状态">
        <template #value>
          <span v-if="demandInfo.ryD_State == 1" class="color-red">未接单</span>
          <span v-if="demandInfo.ryD_State == 2" class="color-blue">已接单</span>
          <span v-if="demandInfo.ryD_State == 3" class="color-yellowGreen">已处理</span>
          <span v-if="demandInfo.ryD_State == 4" class="color-green">已完成</span>
          <span v-if="demandInfo.ryD_State == 5" class="color-green">手动记录</span>
          <span v-if="demandInfo.ryD_State == -2" class="color-red">暂存</span>
        </template>
      </van-cell>
      <van-cell title="类型" :value="demandInfo.ryD_DemandTypeName" />
      <van-cell title="发起时间" :value="dateTimeSpan(demandInfo.ryD_UserStartTime)" />
      <van-cell title="发起人" :value="demandInfo.ryD_UserName" />
      <van-cell title="联系方式" :value="demandInfo.ryD_UserPhone" />
      <van-cell title="位置" :value="demandInfo.ryD_UserAddress" />
      <van-cell title="IP地址" :value="demandInfo.ryD_UserIp" />
    </van-cell-group>
    <van-cell-group inset class="" title="需求详情">
      <van-cell>
        {{ demandInfo.ryD_DemandDetails }}
      </van-cell>
    </van-cell-group>
    <van-cell-group v-if="demandInfo.ryD_State == -2" inset class="" title="回退原因">
      <van-cell>
        {{ demandInfo.ryD_Fallback }}
      </van-cell>
    </van-cell-group>
    <van-cell-group inset class="" title="附件图片">
      <van-cell title="附件图片" v-if="files == ''" />
      <van-grid v-else :column-num="3">
        <template v-for="(item, index) in files" :key="index">
          <van-grid-item @click="ImagePreview({ images: files, startPosition: index })">
            <van-image :src="item" alt="" fit="contain" close-on-popstate="true"></van-image>
          </van-grid-item>
        </template>
        <!-- <van-popup v-model:show="imgShow" @click="imgShowUrl = ''; imgShow = false;">
                    <van-image :src="imgShowUrl" alt="" fit="contain" width="100vw" height="99vh"></van-image>
                </van-popup> -->
      </van-grid>
    </van-cell-group>
    <van-cell-group inset class="" title="处理信息">
      <van-cell title="处理等级">
        <template #value>
          <span v-if="demandInfo.ryD_Level == 1" class="color-blue">一般</span>
          <span v-if="demandInfo.ryD_Level == 2" class="color-yellow">紧急</span>
          <span v-if="demandInfo.ryD_Level == 3" class="color-orange">严重</span>
          <span v-if="demandInfo.ryD_Level == 4" class="color-red">重大</span>
        </template>
      </van-cell>
      <van-cell title="IT负责人"><span style="text-decoration:line-through;color:red;">{{ demandInfo.ryD_OldItName }}
        </span>
        {{ demandInfo.ryD_ItName }}</van-cell>
      <van-cell title="IT负责人联系方式"><span style="text-decoration:line-through;color:red;">{{ demandInfo.ryD_OldItPhone }}
        </span> {{ demandInfo.ryD_ItPhone }}</van-cell>
      <van-cell title="接单时间" :value="dateTimeSpan(demandInfo.ryD_ItStartTime)" />
      <van-cell title="是否转单" :value="demandInfo.ryD_ToItId == null ? '' : '已转单'" />
      <van-cell title="处理完成时间" :value="dateTimeSpan(demandInfo.ryD_ItEndTime)" />
      <van-cell title="用户确认时间" :value="dateTimeSpan(demandInfo.ryD_UserEndTime)" />
      <van-cell title="是否加入知识库" :value="demandInfo.ryD_KBId == null ? '否' : '是'" />
    </van-cell-group>
    <van-cell-group inset class="" title="问题原因">
      <van-cell v-if="demandInfo.ryD_Reason == '' || demandInfo.ryD_Reason == null">
        无
      </van-cell>
      <van-cell v-else>
        {{ demandInfo.ryD_Reason }}
      </van-cell>
    </van-cell-group>
    <van-cell-group inset class="" title="解决方案">
      <van-cell v-if="demandInfo.ryD_Solution == '' || demandInfo.ryD_Solution == null">
        无
      </van-cell>
      <van-cell v-else>
        {{ demandInfo.ryD_Solution }}
      </van-cell>
    </van-cell-group>
    <van-cell-group inset class="" title="服务">
      <van-cell title="本次用户评价星级">
        <template #value>
          <van-image src="/img/pinLunXinSelect.png" width="30" @click="btnImage(index)"
            v-for="index in demandInfo.ryD_Star == null ? 0 : demandInfo.ryD_Star" :key="index">
          </van-image>
          <van-image src="/img/pinLunXin.png" width="30" @click="
            btnImage((demandInfo.ryD_Star == null ? 0 : demandInfo.ryD_Star) + index)
          " v-for="index in demandInfo.ryD_Star == null ? 5 : 5 - demandInfo.ryD_Star" :key="index">
          </van-image>
        </template>
      </van-cell>
      <van-cell title="本次用户评价内容" v-if="demandInfo.ryD_State != 3 || demandInfo.ryD_UserId != RYD_UserId">
        <template #label>
          <span style="font-size: 16px" v-if="demandInfo.ryD_Evaluate == '' || demandInfo.ryD_Evaluate == null">无</span>
          <span style="font-size: 16px" v-else>{{ demandInfo.ryD_Evaluate }}</span>
        </template>
      </van-cell>
      <van-cell title="本次用户评价内容" v-if="demandInfo.ryD_State == 3 && demandInfo.ryD_UserId == RYD_UserId">
        <template #label>
          <van-field v-model="demandInfo.ryD_Evaluate" autosize rows="2" type="textarea" name="ryD_Evaluate"
            placeholder="请填写评价内容" />
        </template>
      </van-cell>
    </van-cell-group>
    <div style="height: 20px"></div>

    <van-overlay :show="loading">
      <div class="wrapper">
        <van-loading type="spinner" size="5vh" vertical color="#fff">加载中...</van-loading>
      </div>
    </van-overlay>

    <van-cell
      v-if="demandInfo.ryD_State == 1 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
      <van-button type="success" block round @click="btnReceivingOrders" size="large">确认接单</van-button>
      <van-popup v-model:show="showFallback" round position="bottom" closeable>
        <van-form @submit="onFallbackOrder" style="margin-top: 3vh">
          <van-cell-group inset>
            <van-field v-model="demandInfo.ryD_Fallback" autosize rows="2" type="textarea" name="ryD_Reason"
              label="回退原因" placeholder="请填写回退原因" :rules="[{ required: true, message: '请输入回退原因' }]" />
          </van-cell-group>
          <div style="margin: 5vw">
            <van-button round block type="primary" native-type="submit">
              提交
            </van-button>
          </div>
        </van-form>
      </van-popup>
    </van-cell>
    <van-cell
      v-if="demandInfo.ryD_State == 2 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
      <van-button type="primary" block round @click="CompletionOrderShow = true" size="large">确认完单</van-button>
      <van-popup v-model:show="CompletionOrderShow" round position="bottom" closeable>
        <van-form @submit="onCompletionOrder" style="margin-top: 3vh">
          <van-cell-group inset>
            <van-field v-model="demandInfo.ryD_Reason" autosize rows="2" type="textarea" name="ryD_Reason" label="问题原因"
              placeholder="请填写问题原因" :rules="[{ required: true, message: '请输入问题原因' }]" />
            <van-field v-model="demandInfo.ryD_Solution" autosize rows="2" type="textarea" name="ryD_Solution"
              label="解决方案" placeholder="请填写解决方案" :rules="[{ required: true, message: '请填写解决方案' }]" />

            <van-field name="switch" label="是否加入知识库">
              <template #input>
                <van-switch v-model="demandInfo.ryD_KBId" />
              </template>
            </van-field>
          </van-cell-group>
          <div style="margin: 5vw">
            <van-button round block type="primary" native-type="submit">
              提交
            </van-button>
          </div>
        </van-form>
      </van-popup>
    </van-cell>
    <van-cell v-if="demandInfo.ryD_State == 3 && demandInfo.ryD_UserId == RYD_UserId">
      <van-button type="primary" block round @click="btnEvaluationOrder" size="large">提交评价</van-button>
    </van-cell>
    <van-cell v-if="demandInfo.ryD_State == -2 && demandInfo.ryD_UserId == RYD_UserId">
      <van-button type="warning" block round @click="btnEditOrders">编辑需求</van-button>
    </van-cell>
    <van-cell v-if="demandInfo.ryD_State == 1 && demandInfo.ryD_UserId == RYD_UserId">
      <van-button type="danger" block round @click="btnCancelOrder">撤销订单</van-button>
    </van-cell>
    <van-cell v-if="demandInfo.ryD_State == -2 && demandInfo.ryD_UserId == RYD_UserId">
      <van-button type="danger" block round @click="btnDeleteOrder">删除需求订单</van-button>
    </van-cell>
    <van-cell>
      <div class="operation">
        <div
          v-if="demandInfo.ryD_State == 1 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
          <van-button type="danger" plain round @click="showFallback = true">回退订单</van-button>
        </div>
        <div
          v-if="demandInfo.ryD_State == 1 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
          <van-button type="warning" round @click="btnTransferOrders">确认转单</van-button>
          <van-popup v-model:show="showTransferOrders" round position="bottom">
            <van-picker :columns="columnsTransferOrders" @cancel="showTransferOrders = false"
              @confirm="onTransferOrders" :loading="loadingTransferOrders" :columns-field-names="{
                text: 'cityName',
              }" />
          </van-popup>
        </div>
        <div
          v-if="demandInfo.ryD_State == 1 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
          <van-button type="danger" round @click="btnCompanyTransferOrders">跨公司转单</van-button>

          <van-popup v-model:show="showCompanyTransferOrders" round position="bottom">
            <van-picker :columns="columnsCompanyTransferOrders" @cancel="showCompanyTransferOrders = false"
              @confirm="onCompanyTransferOrders" :loading="loadingCompanyTransferOrders" :columns-field-names="{
                text: 'cityName',
              }" />
          </van-popup>
        </div>
        <div
          v-if="demandInfo.ryD_State == 1 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
          <van-button type="warning" plain round @click="btnLevelOrder">调整事件等级</van-button>

          <van-popup v-model:show="showLevelOrder" round position="bottom">
            <van-picker :columns="['一般', '紧急', '严重', '重大']" @cancel="showLevelOrder = false" @confirm="onLevelOrder" />
          </van-popup>
        </div>
        <div
          v-if="demandInfo.ryD_State == 1 && (demandInfo.ryD_ToItId == null ? demandInfo.ryD_ItId == RYD_UserId : demandInfo.ryD_ToItId == RYD_UserId)">
          <van-button type="warning" plain round @click="btnTypeOrder">调整需求类别</van-button>

          <van-popup v-model:show="showTypeOrder" round position="bottom">
            <van-picker :columns="DemandTypeList"
              :columns-field-names="{ text: 'ryD_DemandTypeName', value: 'ryD_DemandTypeNo' }"
              :default-index="DemandTypeList.length / 2 > 2 ? 2 : (DemandTypeList.length % 2 == 0 ? DemandTypeList.length / 2 : (DemandTypeList.length - 1) / 2)"
              @cancel="showTypeOrder = false" @confirm="onTypeOrder" />
          </van-popup>
        </div>
      </div>
    </van-cell>

    <van-divider />
  </div>
</template>

<script setup>
// eslint-disable-next-line no-unused-vars
import axios from "@/utils/axios";
// eslint-disable-next-line no-unused-vars
import { inject, nextTick, onMounted, ref } from "vue";
// eslint-disable-next-line no-unused-vars
import { useRoute, useRouter } from "vue-router";

import dateTimeSpan from "@/utils/dateTime";

// eslint-disable-next-line no-unused-vars
import { ImagePreview, Dialog, Toast } from "vant";

let nav = inject("nav");
nav.showLeft = true;
nav.textLeft = "返回";
nav.title = "ITSM 需求详情";
nav.showRight = false;

// eslint-disable-next-line no-unused-vars
const router = useRouter();
const route = useRoute();

let RYD_DemandId = "";
if (route.query != null) {
  RYD_DemandId = route.query.RYD_DemandId;
}
let sionDd = sessionStorage.getItem("RYD_DemandId");
if (sionDd != null && sionDd != "") {
  nav.textLeft = "主页";
  sessionStorage.removeItem("RYD_DemandId");
}

let RYD_UserId = inject("RYD_UserId");

let demandInfo = ref({
  ryD_Company: null,
  ryD_CreateBy: null,
  ryD_CreateTime: null,
  ryD_DemandDetails: null,
  ryD_DemandId: null,
  ryD_Evaluate: null,
  ryD_Files: null,
  ryD_Id: null,
  ryD_ItEndTime: null,
  ryD_ItId: null,
  ryD_ItStartTime: null,
  ryD_KBId: null,
  ryD_Level: null,
  ryD_ModifyBy: null,
  ryD_ModifyTime: null,
  ryD_Reason: null,
  ryD_Solution: null,
  ryD_Star: null,
  ryD_State: null,
  ryD_ToItId: null,
  ryD_TypeId: null,
  ryD_UserAddress: null,
  ryD_UserEndTime: null,
  ryD_UserId: null,
  ryD_UserIp: null,
  ryD_UserName: null,
  ryD_UserPhone: null,
  ryD_UserStartTime: null,
  ryD_Fallback: null,
});

let files = ref([]);

let columnsTransferOrders = ref([]);
let columnsCompanyTransferOrders = ref([]);

let DemandTypeList = ref([]);

let loading = ref(true);
let showFallback = ref(false);

// let imgShow = ref(false);

let loadingTransferOrders = ref(false);
let loadingCompanyTransferOrders = ref(false);
let showTransferOrders = ref(false);
let showCompanyTransferOrders = ref(false);
let CompletionOrderShow = ref(false);
let showLevelOrder = ref(false);
let showTypeOrder = ref(false);

let btnTypeOrder = () => {
  showTypeOrder.value = true;
  if (DemandTypeList.value.length == 0) {
    axios({
      method: 'get',
      url: '/api/DemandType?RYD_Company=' + demandInfo.value.ryD_Company
    }).then(resT => {
      DemandTypeList.value = resT;
    })
  }

}

let btnEditOrders = () => {
  router.push({
    name: "AndroidAddDemand",
    query: {
      RYD_DemandId: demandInfo.value.ryD_DemandId
    }
  })
}
let onTypeOrder = (res) => {
  loading.value = true;
  showFallback.value = false;
  axios({
    method: 'put',
    url: "/api/UpdateTypeOrder",
    data: {
      RYD_UserId: RYD_UserId,
      RYD_DemandId: demandInfo.value.ryD_DemandId,
      RYD_ToType: res.ryD_DemandTypeNo
    }
  }).then(res => {
    loading.value = false;
    if (res == true) {
      Toast.success({ overlay: true, message: "修改成功" });
      setTimeout(() => {
        if(nav.textLeft=="主页"){
            router.replace("AndroidHomePage")
        }else{
            router.back();
        }
      }, 1500);
    } else {
      Toast.fail({ overlay: true, message: "失败" });
    }

  }).catch(() => {
    loading.value = false;
    Toast.fail({ overlay: true, message: "失败" });
  })
}
let onLevelOrder = (res, res2) => {
  loading.value = true;
  showFallback.value = false;
  axios({
    method: 'put',
    url: "/api/setLevelOrder",
    data: {
      RYD_ItId: RYD_UserId,
      RYD_DemandId: demandInfo.value.ryD_DemandId,
      RYD_Level: res2 + 1
    }
  }).then(res => {
    loading.value = false;
    if (res == true) {
      Toast.success({ overlay: true, message: "调整成功" });
      setTimeout(() => {
        if(nav.textLeft=="主页"){
            router.replace("AndroidHomePage")
        }else{
            router.back();
        }
      }, 1500);
    } else {
      Toast.fail({ overlay: true, message: "失败" });
    }

  }).catch(() => {
    loading.value = false;
    Toast.fail({ overlay: true, message: "失败" });
  })
}
let btnLevelOrder = () => {
  showLevelOrder.value = true;
}
//退单功能
let onFallbackOrder = () => {

  loading.value = true;
  showFallback.value = false;

  axios({
    method: 'put',
    url: "/api/FallbackOrder",
    data: {
      RYD_UserId: RYD_UserId,
      RYD_DemandId: demandInfo.value.ryD_DemandId,
      RYD_Fallback: demandInfo.value.ryD_Fallback,
      RYD_Company: demandInfo.value.ryD_Company
    }
  }).then(res => {
    loading.value = false;
    if (res == true) {
      Toast.success({ overlay: true, message: "回退成功" });
      setTimeout(() => {
        if(nav.textLeft=="主页"){
            router.replace("AndroidHomePage")
        }else{
            router.back();
        }
      }, 1500);
    } else {
      Toast.fail({ overlay: true, message: "失败" });
    }

  }).catch(() => {
    loading.value = false;
    Toast.fail({ overlay: true, message: "失败" });
  })
}

let btnEvaluationOrder = () => {
  if (demandInfo.value.ryD_Star < 2 && (demandInfo.value.ryD_Evaluate == "" || demandInfo.value.ryD_Evaluate == null)) {

    Dialog.alert({
      message: '请填写低分原因',
      title: "提示"
    });
    return;
  }
  Dialog.confirm({
    title: "提示",
    message: "确认提交评价吗？",
  })
    .then(() => {
      loading.value = true;
      axios({
        method: "put",
        url: "/api/EvaluationOrder",
        data: {
          RYD_UserId: RYD_UserId,
          RYD_DemandId: demandInfo.value.ryD_DemandId,
          RYD_Star: demandInfo.value.ryD_Star,
          RYD_Evaluate: demandInfo.value.ryD_Evaluate,
        },
      })
        .then((res) => {
          loading.value = false;
          if (res == true) {
            Toast.success({ overlay: true, message: "评价成功" });
            setTimeout(() => {
              if(nav.textLeft=="主页"){
                  router.replace("AndroidHomePage")
              }else{
                  router.back();
              }
            }, 1500);
          } else {
            Toast.fail({ overlay: true, message: "失败" });
          }
        })
        .catch(() => {
          loading.value = false;
          Toast.fail({ overlay: true, message: "失败" });
        });
      // on confirm
    })
    .catch(() => {
      // on cancel
    });
};
let btnImage = (res) => {
  if (demandInfo.value.ryD_State == 3 && demandInfo.value.ryD_UserId == RYD_UserId) {
    demandInfo.value.ryD_Star = res;
  }
};

let btnCompanyTransferOrders = () => {
  showCompanyTransferOrders.value = true;
  if (columnsCompanyTransferOrders.value.length == 0) {
    loadingCompanyTransferOrders.value = true;

    axios({
      method: 'get',
      url: '/api/GetCompanyTransferOrdersUsers?RYD_Company=' + demandInfo.value.ryD_Company
    }).then(resUser => {
      var value = [];
      resUser.forEach(element => {
        if (element.ryD_CompanyNo != demandInfo.value.ryD_Company) {
          let children = [];
          element.childrenT1.forEach(item => {
            children.push({
              id: item.ryD_TypeNo, cityName: item.ryD_TypeName
            })
          })
          if (children.length > 0) {
            value.push({ id: element.ryD_CompanyNo, cityName: element.ryD_CompanyName, children: children });
          }
        }
      });
      columnsCompanyTransferOrders.value = value;
      loadingCompanyTransferOrders.value = false;
    })
    // axios({
    //   method: "get",
    //   url: "/api/SetCompany",
    // }).then((res) => {
    //   if (res != null) {
    //     let value = [];
    //     res.forEach((item) => {
    //       if (item.ryD_CompanyNo != demandInfo.value.ryD_Company) {
    //         let children = [];
    //         axios({
    //           method: "get",
    //           url: "/api/DemandType?RYD_Company=" + item.ryD_CompanyNo,
    //         })
    //           .then((resType) => {
    //             resType.forEach((element) => {
    //               children.push({
    //                 id: element.ryD_DemandTypeNo,
    //                 cityName: element.ryD_DemandTypeName,
    //               });
    //             });
    //             value.push({
    //               id: item.ryD_CompanyNo,
    //               cityName: item.ryD_CompanyName,
    //               children: children,
    //             });
    //           })
    //           .then(() => {
    //             loadingCompanyTransferOrders.value = false;
    //             columnsCompanyTransferOrders.value = value;
    //           });
    //       }
    //     });
    //   }
    // });
  }
};
let onCompanyTransferOrders = (res) => {
  loading.value = true;
  loadingCompanyTransferOrders.value = false;
  axios({
    method: "put",
    url: "/api/CompanyTransferOrders",
    data: {
      RYD_UserId: RYD_UserId,
      RYD_DemandId: demandInfo.value.ryD_DemandId,
      RYD_ToCompany: res[0].id,
      RYD_ToType: res[1].id,
    },
  })
    .then((res) => {
      loading.value = false;
      if (res == true) {
        Toast.success({ overlay: true, message: "转单成功" });
        setTimeout(() => {
          if(nav.textLeft=="主页"){
              router.replace("AndroidHomePage")
          }else{
              router.back();
          }
        }, 1500);
      } else {
        Toast.fail({ overlay: true, message: "失败" });
      }
    })
    .catch(() => {
      loading.value = false;
      Toast.fail({ overlay: true, message: "失败" });
    });
};
let btnTransferOrders = () => {
  showTransferOrders.value = true;
  if (columnsTransferOrders.value.length == 0) {
    loadingTransferOrders.value = true;
    // GetDemandTypes
    axios({
      method: 'get',
      url: '/api/GetTransferOrdersUsers?RYD_Company=' + demandInfo.value.ryD_Company
    }).then(resUser => {
      var value = [];
      resUser.forEach(element => {
        let children = [];
        element.children.forEach(item => {
          if (item.ryD_UserId != RYD_UserId) {
            children.push({
              id: item.ryD_UserId, cityName: "姓名：" + item.ryD_UserName + "，正在处理单数：" + item.num + ""
            })
          }
        })
        if (children.length > 0) {
          value.push({ id: element.ryD_TypeNo, cityName: "类别：" + element.ryD_TypeName, children: children });
        }
      });
      columnsTransferOrders.value = value;
      loadingTransferOrders.value = false;
    })
  }
};

let onTransferOrders = (res) => {
  loading.value = true;
  loadingTransferOrders.value = false;
  if (res == null) {
    return;
  }
  axios({
    method: "put",
    url: "/api/TransferOrders",
    data: {
      RYD_UserId: RYD_UserId,
      RYD_DemandId: demandInfo.value.ryD_DemandId,
      RYD_ToUserId: res[1].id,
      RYD_ToType: res[0].id,
    },
  })
    .then((res) => {
      loading.value = false;
      if (res == true) {
        Toast.success({ overlay: true, message: "转单成功" });
        setTimeout(() => {
          if(nav.textLeft=="主页"){
              router.replace("AndroidHomePage")
          }else{
              router.back();
          }
        }, 1500);
      } else {
        Toast.fail({ overlay: true, message: "失败" });
      }
    })
    .catch(() => {
      loading.value = false;
      Toast.fail({ overlay: true, message: "失败" });
    });
};

let onCompletionOrder = () => {
  loading.value = true;
  CompletionOrderShow.value = false;

  axios({
    method: "put",
    url: "/api/CompletionOrder",
    data: {
      RYD_UserId: RYD_UserId,
      RYD_DemandId: demandInfo.value.ryD_DemandId,
      RYD_Reason: demandInfo.value.ryD_Reason,
      RYD_Solution: demandInfo.value.ryD_Solution,
      RYD_KBId: demandInfo.value.ryD_KBId + "",
      RYD_Company: demandInfo.value.ryD_Company,
    },
  })
    .then((res) => {
      loading.value = false;
      if (res == true) {
        Toast.success({ overlay: true, message: "完单成功" });
        setTimeout(() => {
          if(nav.textLeft=="主页"){
              router.replace("AndroidHomePage")
          }else{
              router.back();
          }
        }, 1500);
      } else {
        Toast.fail({ overlay: true, message: "失败" });
      }
    })
    .catch(() => {
      loading.value = false;
      Toast.fail({ overlay: true, message: "失败" });
    });
};
let btnCancelOrder = () => {
  Dialog.confirm({
    title: "提示",
    message: "确认撤销该订单吗？",
  })
    .then(() => {
      loading.value = true;
      axios({
        method: "put",
        url: "/api/CancelOrder",
        data: {
          RYD_UserId: RYD_UserId,
          RYD_DemandId: demandInfo.value.ryD_DemandId,
        },
      })
        .then((res) => {
          loading.value = false;
          if (res == true) {
            Toast.success({ overlay: true, message: "撤销成功，请前往暂存查看" });
            setTimeout(() => {
              if(nav.textLeft=="主页"){
                  router.replace("AndroidHomePage")
              }else{
                  router.back();
              }
            }, 1500);
          } else {
            Toast.fail({ overlay: true, message: "失败" });
          }
        })
        .catch(() => {
          loading.value = false;
          Toast.fail({ overlay: true, message: "失败" });
        });
      // on confirm
    })
    .catch(() => {
      // on cancel
    });
};
let btnDeleteOrder = () => {
  Dialog.confirm({
    title: "提示",
    message: "确认删除该订单吗？",
  })
    .then(() => {
      loading.value = true;
      axios({
        method: "put",
        url: "/api/DeleteOrder",
        data: {
          RYD_UserId: RYD_UserId,
          RYD_DemandId: demandInfo.value.ryD_DemandId,
        },
      })
        .then((res) => {
          loading.value = false;
          if (res == true) {
            Toast.success({ overlay: true, message: "删除成功" });
            setTimeout(() => {
              if(nav.textLeft=="主页"){
                  router.replace("AndroidHomePage")
              }else{
                  router.back();
              }
            }, 1500);
          } else {
            Toast.fail({ overlay: true, message: "失败" });
          }
        })
        .catch(() => {
          loading.value = false;
          Toast.fail({ overlay: true, message: "失败" });
        });
      // on confirm
    })
    .catch(() => {
      // on cancel
    });
};

let btnReceivingOrders = () => {
  Dialog.confirm({
    title: "提示",
    message: "确认接取该订单吗？",
  })
    .then(() => {
      loading.value = true;
      axios({
        method: "put",
        url: "/api/ReceivingOrders",
        data: {
          RYD_UserId: RYD_UserId,
          RYD_DemandId: demandInfo.value.ryD_DemandId,
        },
      })
        .then((res) => {
          loading.value = false;
          if (res == true) {
            Toast.success({ overlay: true, message: "接单成功" });
            setTimeout(() => {
              if(nav.textLeft=="主页"){
                  router.replace("AndroidHomePage")
              }else{
                  router.back();
              }
            }, 1500);
          } else {
            Toast.fail({ overlay: true, message: "失败" });
          }
        })
        .catch(() => {
          loading.value = false;
          Toast.fail({ overlay: true, message: "失败" });
        });
      // on confirm
    })
    .catch(() => {
      // on cancel
    });
};

let initData = () => {
  Promise.all([
    axios({
      method: "get",
      url: "/api/DemandInfo?RYD_DemandId=" + RYD_DemandId,
    }).then((res) => {
      demandInfo.value = res;
      if (demandInfo.value.ryD_Files != null) {
        files.value = demandInfo.value.ryD_Files.split("|");
      }
      if (files.value.length > 0) {
        if (
          files.value[files.value.length - 1] == "|" ||
          files.value[files.value.length - 1] == ""
        ) {
          files.value.splice(files.value.length - 1);
        }
      }
      loading.value = false;
    }),
    // axios({
    //     method: 'get',
    //     url: '/api/SetCompany'
    // }).then(res => {
    //     companyList.value = res;
    // }),
  ]).then(() => { });
};

onMounted(() => {
  nextTick(function () {
    // 仅在整个视图都被渲染之后才会运行的代码
    initData();
  });
});
</script>

<style scoped>
.color-blue {
  color: #466ca3;
}

.color-yellow {
  color: rgb(255, 102, 0);
}

.color-orange {
  color: rgb(255, 187, 0);
}

.color-red {
  color: red;
}

.color-green {
  color: green;
}

.color-yellowGreen {
  color: yellowgreen;
}

.operation>div {
  float: left;
  margin: 5px 2%;
}
</style>
